#include "all.h"

/******************************************************************************************************************************
cnt = 0 对应占空比0
TIM3:(部分重映像)
CH1:PB4
*******************************************************************************************************************************/
void TIM_PWM_Init(void)
{
	//设置变量
	GPIO_InitType GPIO_InitStructure;
    TIM_TimeBaseInitType TIM_TimeBaseStructure;
	OCInitType TIM_OCInitStructure; 
	
	//使能时钟
	RCC_EnableAPB1PeriphClk( RCC_APB1_PERIPH_TIM3  , ENABLE);
	RCC_EnableAPB2PeriphClk(  RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
	GPIO_ConfigPinRemap( GPIO_PART1_RMP_TIM3 , ENABLE);//使定时器TIM3进行部分重映像操作
	 
	TIM_TimeBaseStructure.Period = 1000;
	TIM_TimeBaseStructure.Prescaler = 720;
	TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
	TIM_TimeBaseStructure.ClkDiv = 0;
	TIM_InitTimeBase(TIM3 , &TIM_TimeBaseStructure);
	
	//端口复用
	GPIO_InitStructure.Pin = GPIO_PIN_4 ;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 	
	GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);   
	
	
	//PB4:TIM3_CH1
	TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
	TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
	TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
	TIM_OCInitStructure.Pulse = 0;
	TIM_InitOc1(TIM3 , &TIM_OCInitStructure);
	TIM_ConfigOc1Preload(TIM3 , TIM_OC_PRE_LOAD_ENABLE);
	
	TIM_Enable(TIM3, ENABLE);
}


